import { Attention, PrivateMsg } from "@entity/chat";
import { SystemMsg } from "@entity/system_msg";
import { Project } from '@entity/project';
import { FriendInfo, User } from '@entity/user';
import { FIELD, getCache, getMapCache, setCache, setMapCache, myId } from "./cache";
import user_axios from '@axios/user';
import ws_axios from '@axios/websocket';

export function storageMe(state: any, me: User): void {
	state.localify.me = me;
	setCache(FIELD.ME(), me, 30);
}

export function getMe(state: any): User {
	if (state.localify.me === undefined) {
		state.localify.me = getCache(FIELD.ME());
	}
	return state.localify.me;
}

export function storageAllProject(state: any, projects: Array<Project>): void {
	let id: number = myId(state);
	if (id == -1) {
		return;
	}
	let map: Map<number, Project> = getProjectMap(state);
	projects.forEach((item: Project) => map.set(item.id, item));
	state.localify.projects = map;
	setMapCache(FIELD.PROJECT_MAP(id), map);
}

export function getProjectMap(state: any): Map<number, Project> {
	let id: number = myId(state);
	if (id == -1) {
		return new Map<number, Project>();
	}
	if (state.localify.project_map === undefined) {
		state.localify.project_map = getMapCache(FIELD.PROJECT_MAP(id));
	}
	return state.localify.project_map;
}

 export function storageUser(state: any, user: User): void {
	let id: number = myId(state);
	if (id == -1) {
		return;
	}
	let map: Map<number, User> = getUserMap(state);
	map.set(user.id, user);
	state.localify.user_map = map;
	setMapCache(FIELD.USER_MAP(id), map);
}

export function storageAllUser(state: any, users: Array<User>): void {
	let id: number = myId(state);
	if (id == -1) {
		return;
	}
	let map: Map<number, User> = getUserMap(state);
	users.forEach((item: User) => map.set(item.id, item));
	state.localify.user_map = map;
	setMapCache(FIELD.USER_MAP(id), map);
}

 export function getUserMap(state: any): Map<number, User> {
	let id: number = myId(state);
	if (id == -1) {
		return new Map<number, User>();
	}
	if (state.localify.user_map === undefined) {
		state.localify.user_map = getMapCache(FIELD.USER_MAP(id));
	}
	return state.localify.user_map;
}

export async function storagePrivateMsg(state: any, privateMsg: PrivateMsg): Promise<void> {
	/** 获取本地缓存 */
	let my_id: number = myId(state);
	if (my_id == -1) {
		return;
	}
	let map: Map<number, Attention> = getPrivateMsgMap(state);
	/** 获取和对方的所有聊天记录 */
	let id: number = privateMsg.sender == my_id ? privateMsg.receiver : privateMsg.sender;
	let attention: Attention = map.get(id);
	if (attention == undefined) {
		let sender: User = getUserMap(state).get(id);
		if (sender == undefined) {
			await user_axios.base.queryById(id).then((res: any) => {
				sender = res.user;
				storageUser(state, res.user);
			})
		}
		attention = new Attention(sender);
	}
	if (!attention.msgList.contain('msgId', privateMsg.msgId)) {
		attention.msgList.push(privateMsg);
	}
	attention.draft = '';
	map.set(id, attention);
	state.localify.private_msg_map = map;
	setMapCache(FIELD.USER_MESSAGE_MAP(my_id), map);
}

 export function getPrivateMsgMap(state: any): Map<number, Attention> {
	let id: number = myId(state);
	if (id == -1) {
		return new Map<number, Attention>();
	}
	if (state.localify.private_msg_map === undefined) {
		state.localify.private_msg_map = getMapCache(FIELD.USER_MESSAGE_MAP(id));
	}
	return state.localify.private_msg_map;
}

 export function storageSystemMsg(state: any, msg: SystemMsg): void {
	let id: number = myId(state);
	if (id == -1) {
		return;
	}
	let map: Map<number, SystemMsg> = getSystemMsgMap(state);
	msg.data = JSON.parse(msg.data);
	map.set(msg.id, msg);
	state.localify.system_msg_map = map;
	setMapCache(FIELD.SYSTEM_MESSAGE_MAP(id), map);
}

export function storageAllSystemMsg(state: any, msgList: Array<SystemMsg>): void {
	let id: number = myId(state);
	if (id == -1) {
		return;
	}
	msgList.forEach((msg: SystemMsg) => {
		storageSystemMsg(state, msg);
	})
}

 export function getSystemMsgMap(state: any): any {
	let id: number = myId(state);
	if (id == -1) {
		return new Map<number, SystemMsg>();
	}
	if (state.localify.system_msg_map === undefined) {
		let cache :Map<any, any> = getMapCache(FIELD.SYSTEM_MESSAGE_MAP(id));
		state.localify.system_msg_map = cache;
		if (cache.size == 0) {
			ws_axios.system.readSystemMsg(id, false, 10).then((msgList: Array<SystemMsg>) => {
				storageAllSystemMsg(state, msgList);
				state.localify.system_msg_map = getMapCache(FIELD.SYSTEM_MESSAGE_MAP(id));
			})
		}
		
	}
	return state.localify.system_msg_map;
}

export function storageFriend(state: any, friendInfo: FriendInfo): void {
   let id: number = myId(state);
   if (id == -1) {
	   return;
   }
   let map: Map<number, FriendInfo> = getFriendMap(state);
   map.set(friendInfo.id, friendInfo);
   state.localify.friend_map = map;
   setMapCache(FIELD.FRIEND_MAP(id), map);
}

export function getFriendMap(state: any): Map<number, FriendInfo> {
   let id: number = myId(state);
   if (id == -1) {
	   return new Map<number, FriendInfo>();
   }
   if (state.localify.friend_map === undefined) {
	   state.localify.friend_map = getMapCache(FIELD.FRIEND_MAP(id));
   }
   return state.localify.friend_map;
}